package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;


@Component
@Path("/v{version}/book")
public class BookRestController {
	
	@Resource
	private BookService bookService;
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/info")
	public String getInfo(@QueryParam("book_id")String book_id,@HeaderParam("token")String token){
		try {
			book_id=IReadUtils.preventInjection(book_id);
			String ret = bookService.getInfo(book_id,token);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "获取书籍详情异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
    @Path("/order/pay")
	public String getOrderPay(@FormParam("chapter_id")int chapter_id,
			@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookService.updateOrderPay(chapter_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("chapter_id", chapter_id);
			QSReadException.printf(e, "获取书籍详情异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 赠送金票
	 * @param book_id
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
	@Path("/present")
	public String present(@FormParam("book_id")int book_id,@FormParam("num")int num,
			@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			num=1;
			String ret = bookService.updatePresentVotes(book_id,num,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("num", num);
			QSReadException.printf(e, "获取书籍详情异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/chapter/list")
	public String getChapterList(@QueryParam("book_id")String book_id,
			@QueryParam("pages")int pages,@QueryParam("contents")int contents,@QueryParam("order")String order,
			@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			book_id=IReadUtils.preventInjection(book_id);
			order=IReadUtils.preventInjection(order);
			String ret = bookService.getChapterList(book_id,pages,contents,order,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/chapters")
	public String getChapters(@QueryParam("book_id")String book_id,@QueryParam("order")String order,
			@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			book_id=IReadUtils.preventInjection(book_id);
			order=IReadUtils.preventInjection(order);
			String ret = bookService.getChapters(book_id,order,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 设置自动订购
	 * @param book_id
	 * @param status
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/auto")
	public String updateBookAuto(@FormParam("book_id")int book_id,@FormParam("status")int status,
			@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookService.updateBookAuto(book_id,status,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 打赏
	 * @param book_id
	 * @param num
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
	@Path("/reward")
	public String rewardBook(@FormParam("book_id")int book_id,
			@FormParam("num")int num,@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookService.addRewardBook(book_id,num,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("num", num);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
	@Path("/reward/list")
	public String rewardList(@FormParam("book_id")int book_id,
			@QueryParam("pages")int pages,@QueryParam("contents")int contents){
		try {
			String ret = bookService.getBookRewardList(book_id, pages, contents);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	
}
